<?php
namespace app\common\library;

use TencentCloud\Common\Credential;
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;

class TencentSms {
    private $secretId;
    private $secretKey;
    private $sdkAppId;

    public function __construct($secretId, $secretKey, $sdkAppId) {
        $this->secretId = $secretId;
        $this->secretKey = $secretKey;
        $this->sdkAppId = $sdkAppId;
    }

    public function send($phoneNumber, $templateId, $templateParams, $signName) {
        try {
            $cred = new Credential($this->secretId, $this->secretKey);
            $client = new SmsClient($cred, "ap-guangzhou");
            $req = new SendSmsRequest();
            $req->SmsSdkAppId = $this->sdkAppId;
            $req->SignName = $signName;
            $req->TemplateId = $templateId;
            $req->PhoneNumberSet = ["+86{$phoneNumber}"];
            $req->TemplateParamSet = $templateParams;
            $resp = $client->SendSms($req);
            return json_decode($resp->toJsonString(), true);
        } catch (\Exception $e) {
            return ['error' => $e->getMessage()];
        }
    }
}

